package com.core.springSecurity.service;

import com.core.springSecurity.common.SecurityUtils;
import com.model.pojo.sys.SysRole;
import com.model.pojo.sys.SysUser;
import com.service.sys.SysRoleService;
import com.service.sys.SysUserRoleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author: zql
 * @Description: 用户权限处理服务
 * @Data: 17:03 2021/7/28
 */
@Component
public class SysPermissionService {

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user,Set<SysRole> sysRoleSet) {
        Set<String> permissions = new HashSet<>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin(user.getSysUserId())) {
            permissions.add(PermissionService.ALL_PERMISSION);
        } else {
            permissions.addAll(SysRoleService.getApiKeySet(sysRoleSet));
        }
        return permissions;
    }

    public Set<String> getRoleButton(SysUser user,Set<SysRole> sysRoleSet) {
        Set<String> permissions = new HashSet<>();
        // 管理员拥有所有权限
        if (SecurityUtils.isAdmin(user.getSysUserId())) {
            permissions.add(PermissionService.ALL_PERMISSION);
        } else {
            permissions.addAll(SysRoleService.getButtonKeySet(sysRoleSet));
        }
        return permissions;
    }


    /**
     * 获取角色列表
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<SysRole> getRoles(SysUser user) {
        return new HashSet<>(sysUserRoleService.getRoleList(user.getSysUserId()));
    }
}
